//
//  MusicRecommendTableViewController.m
//  XMLY
//
//  Created by lanou on 15/11/4.
//  Copyright © 2015年 曾杰. All rights reserved.
//

#import "MusicRecommendTableViewController.h"
#import "PKRequestManager.h"
#import "MusicViewCell.h"
@interface MusicRecommendTableViewController ()
@property (nonatomic, strong)NSMutableArray * rowArr;
@property (nonatomic, strong)NSMutableArray * sectionArr;
@property (nonatomic, strong)NSMutableDictionary * valueDic;
@property (nonatomic, strong) UIView * headview;
@end

@implementation MusicRecommendTableViewController

-(NSMutableDictionary *)valueDic
{
    if (_valueDic == nil) {
        self.valueDic = [NSMutableDictionary dictionaryWithCapacity:0];
    }
    return _valueDic;
}

-(NSMutableArray *)sectionArr
{
    if (_sectionArr == nil) {
        self.sectionArr = [NSMutableArray arrayWithCapacity:0];
    }
    return _sectionArr;
}

-(void)requestData
{

    [PKRequestManager requestWithType:GET urlString:@"http://mobile.ximalaya.com/mobile/discovery/v2/category/recommends?categoryId=2&contentType=album&device=iPhone&scale=2&version=4.3.20" parDic:nil finish:^(NSData *data) {
        NSDictionary * dic =[ NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
        NSDictionary * dict = [dic objectForKey:@"categoryContents"];
        NSArray * array = [dict objectForKey:@"list"];
        for (int i = 1 ; i<array.count; i++) {
            NSString * title = array[i][@"title"];
            [self.sectionArr addObject:title];
            NSArray * list = array[i][@"list"];
            _rowArr = [NSMutableArray arrayWithCapacity:0];
            for (NSDictionary * dictt in list) {
                MusicModel * model = [[MusicModel alloc]init];
                [model setValuesForKeysWithDictionary:dictt];
                [self.rowArr addObject:model];
            }
            [self.valueDic setObject:self.rowArr forKey:title];
        }
        [self.tableView reloadData];
        
        } error:^(NSError *error) {
        NSLog(@"%@",error);
    }];
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    [self requestData];
    [self.tableView registerClass:[MusicViewCell class] forCellReuseIdentifier:@"cell"];
    self.tableView.rowHeight =self.view.bounds.size.height/6+20;
    self.tableView.sectionHeaderHeight = 30;
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.sectionArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return [self.valueDic[self.sectionArr[section]] count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   MusicViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    MusicModel * model = self.valueDic[self.sectionArr[indexPath.section]] [indexPath.row];
    [cell configMusicCellWithModel:model];
    return cell;
}


-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (tableView==self.tableView) {
        
    
    _headview = [[UIView alloc]initWithFrame:CGRectMake(0, 0,self.view.frame.size.width,20)];
   
    UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(10, 5, 140, 20)];
    label.text = self.sectionArr[section];
    [_headview addSubview:label];
        
        
    UILabel * moreLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.view.frame.size.width-80, 5, 40, 20)];
        moreLabel.backgroundColor = [UIColor whiteColor];
    moreLabel.text = @"更多";
    [_headview addSubview:moreLabel];
        
    UIButton * button = [UIButton buttonWithType:(UIButtonTypeCustom)];
    button.frame = CGRectMake(self.view.frame.size.width-40, 5, 20, 20);
    [button setBackgroundImage:[UIImage imageNamed:@"xiayige"] forState:(UIControlStateNormal)];
    [_headview addSubview:button];
   
    }
     return _headview;
}
//设置区头随tableView滑动而滑动
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{

    if (scrollView==self.tableView) {
        CGFloat sectionHeight = 30;
        if (scrollView.contentOffset.y<=sectionHeight&&scrollView.contentOffset.y>0) {
            scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
        }
        else if (scrollView.contentOffset.y>=sectionHeight)
        {
            scrollView.contentInset = UIEdgeInsetsMake(-sectionHeight, 0, 0, 0);
        }
    }
    
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
